function _(id) {
    if (typeof id == "string") {
        return document.getElementById(id);
    }
    return id;
}
function senfe(o, a, b, c, d) {
    if (!document.getElementById(o)) {
        return false;
    }
    var t = document.getElementById(o).getElementsByTagName("tr");
    for (var i = 0; i < t.length; i++) {
        t[i].style.backgroundColor = (t[i].sectionRowIndex % 2 == 0) ? a: b;
        t[i].onclick = function() {
            if (this.x != "1") {
                this.x = "1"; //本來打算直接用背景色判斷，FF獲取到的背景是RGB值，不好判斷
                this.style.backgroundColor = d;
            } else {
                this.x = "0";
                this.style.backgroundColor = (this.sectionRowIndex % 2 == 0) ? a: b;
            }
        };
        t[i].onmouseover = function() {
            if (this.x != "1") this.style.backgroundColor = c;
        };
        t[i].onmouseout = function() {
            if (this.x != "1") this.style.backgroundColor = (this.sectionRowIndex % 2 == 0) ? a: b;
        };
    }
}
function resizePics(a, w, h) {
    var b = a;
    var c = new Image();
    c.src = b.src;
    if (! (c.width > 0 && c.height > 0)) {
        return;
    };
    if (c.width / c.height >= w / h) {
        if (c.width > w) {
            b.width = w;
            b.height = c.height * w / c.width;
        } else {
            b.width = c.width;
            b.height = c.height;
        }
    } else {
        if (c.height > h) {
            b.height = h;
            b.width = (c.width * h) / c.height;
        } else {
            b.width = c.width;
            b.height = c.height;
        }
    }
}
/* *
 * 显示载入信息
 */
function showLoader() {
    document.getElementsByTagName('body').item(0).style.cursor = "wait";
    if (top.frames['header-frame']) {
        top.frames['header-frame'].document.getElementById("load-div").style.display = "block";
    } else {
        var obj = document.getElementById('loader');
        if (!obj && process_request) {
            obj = document.createElement("DIV");
            obj.id = "loader";
            obj.innerHTML = process_request;
            document.body.appendChild(obj);
        }
    }
}
/* *
 * 隐藏载入信息
 */
function hideLoader() {
    document.getElementsByTagName('body').item(0).style.cursor = "auto";
    if (top.frames['header-frame']) {
        setTimeout(function() {
            top.frames['header-frame'].document.getElementById("load-div").style.display = "none";
        },
        10);
    } else {
        try {
            var obj = document.getElementById("loader");
            obj.style.display = 'none';
            document.body.removeChild(obj);
        } catch(ex) {}
    }
} //清除空格
function trim(text) {
    if (typeof(text) == "string") {
        return text.replace(/^\s*|\s*$/g, "");
    } else {
        return text;
    }
}

function hidden(obj_str, check) {
    if (check) {
        $("#" + obj_str).show();
    } else {
        $("#" + obj_str).hide();
    }
}
function FormatNumber(srcStr, nAfterDot) {
    var srcStr, nAfterDot;
    var resultStr, nTen;
    srcStr = "" + srcStr + "";
    strLen = srcStr.length;
    dotPos = srcStr.indexOf(".", 0);
    if (dotPos == -1) {
        resultStr = srcStr + ".";
        for (i = 0; i < nAfterDot; i++) {
            resultStr = resultStr + "0";
        }
        return resultStr;
    } else {
        if ((strLen - dotPos - 1) >= nAfterDot) {
            nAfter = dotPos + nAfterDot + 1;
            nTen = 1;
            for (j = 0; j < nAfterDot; j++) {
                nTen = nTen * 10;
            }
            resultStr = Math.round(parseFloat(srcStr) * nTen) / nTen;
            return resultStr;
        } else {
            resultStr = srcStr;
            for (i = 0; i < (nAfterDot - strLen + dotPos + 1); i++) {
                resultStr = resultStr + "0";
            }
            return resultStr;
        }
    }
}
function GetMessageLength(str) {
    var oEditor = FCKeditorAPI.GetInstance(str);
    var oDOM = oEditor.EditorDocument;
    var iLength;
    if (document.all) {
        iLength = oDOM.body.innerText.length;
    } else {
        var r = oDOM.createRange();
        r.selectNodeContents(oDOM.body);
        iLength = r.toString().length;
    }
    return iLength;
}
function set_on(o) {
    //o = 'left-nav';
	if (!document.getElementById(o))return false;
	var a = document.getElementById(o).getElementsByTagName("a");
    var location_url = window.location.toString();
    for (var i=0; i< a.length; i++) {
        if(location_url==a[i].href){
            a[i].parentNode.className = 'on';
            return ;
        }
    }
}
function is_ie() {
    return !-[1,];
}



//To Validate Tax ID
function ValidateTaxID(sTaxID){

	//alert('aaa');
 try
 {
  var i;
  var a1;
        var a2;
        var a3;
        var a4;
        var a5;

        var b1;
        var b2;
        var b3;
        var b4;
        var b5;

        var c1;
        var c2;
        var c3;
        var c4;

        var d1;
        var d2;
        var d3;
        var d4;
        var d5;
        var d6;
        var d7;
        var cd8;

  if(sTaxID.length != 8){
  	alert(sTaxID.length);
  	return false;
  }
  
  
  var c;
  for (i = 0; i < 8; i++)
  {
   c = sTaxID.charAt(i);
   if ("0123456789".indexOf(c) == -1) {
   	alert('a');
   	return false;
   }
  }

        d1 	= parseInt(sTaxID.charAt(0));
        d2 	= parseInt(sTaxID.charAt(1));
        d3 	= parseInt(sTaxID.charAt(2));
        d4 	= parseInt(sTaxID.charAt(3));
        d5 	= parseInt(sTaxID.charAt(4));
        d6 	= parseInt(sTaxID.charAt(5));
        d7 	= parseInt(sTaxID.charAt(6));
        cd8 = parseInt(sTaxID.charAt(7));

        c1 	= d1;
        c2 	= d3;
        c3 	= d5;
        c4 	= cd8;

        a1 	= parseInt((d2 * 2) / 10);
        b1 	= (d2 * 2) % 10;

        a2 	= parseInt((d4 * 2) / 10);
        b2 	= (d4 * 2) % 10;

        a3 	= parseInt((d6 * 2) / 10);
        b3 	= (d6 * 2) % 10;

        a4 	= parseInt((d7 * 4) / 10);
        b4 	= (d7 * 4) % 10;

        a5 	= parseInt((a4 + b4) / 10);
        b5 	= (a4 + b4) % 10;

        if((a1 + b1 + c1 + a2 + b2 + c2 + a3 + b3 + c3 + a4 + b4 + c4) % 10 == 0) {
        	return true;
        }
        
        if(d7 = 7)
        {
            if((a1 + b1 + c1 + a2 + b2 + c2 + a3 + b3 + c3 + a5 + c4) % 10 == 0) return true;
        }

        return false;
 }catch(e){
  return false;
 }
}

$("#unicode").change(function(){
	var unid;
	alert($(this).val());
	unid=ValidateTaxID( $(this).val() );

	if(unid){
		alert("ok");
	}else{
		alert("no");
	}
});

function checkunicode(id){

/*
	var regexp=/^\d{8}$/;
	var unicode=$("#"+id).val();
	if(regexp.test(unicode)){
		var d1 =	parseInt(unicode.substr(0,1))*1;
		var d2 =	parseInt(unicode.substr(1,1))*2;
		var d3 =	parseInt(unicode.substr(2,1))*1;
		var d4 =	parseInt(unicode.substr(3,1))*2;
		var d5 =	parseInt(unicode.substr(4,1))*1;
		var d6 =	parseInt(unicode.substr(5,1))*2;
		var d7 =	parseInt(unicode.substr(6,1))*4;
		var d8 =	parseInt(unicode.substr(7,1))*1;
		//alert("統一編碼輸入錯誤");
		//$("#"+id).focus();
	}else{
		if(unicode.length>0){
			alert("統一編碼輸入錯誤");			
			setTimeout(function() {
			   $("#"+id).focus();
			}, 0);
		}
		
	}
*/
}


function checkidnumber(){
	var code=new Array();
	code[10]="A";
	code[11]="B";
	code[12]="C";
	code[13]="D";
	code[14]="D";
	code[15]="E";
	code[16]="F";
	code[17]="G";
	code[34]="I";
	code[18]="J";
	code[19]="K";
	code[20]="L";
	code[21]="M";
	code[22]="N";
	code[35]="O";
	code[23]="P";
	code[24]="Q";
	code[25]="R";
	code[26]="S";
	code[27]="T";
	code[28]="U";
	code[29]="V";
	code[32]="W";
	code[30]="X";
	code[31]="Y";
	code[33]="Z";
	var regexp=/^[A-Za-z][1-3][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
	var idNumber=$("#id_number").val();
	var x="",x1="",x2="",y=0,checksum=0;
	if(regexp.test(idNumber)){
		for(var i in code){
			if(code[i]==idNumber.substr(0,1).toUpperCase())
				x=i;
		}
		x1=parseInt(x.substr(0,1));
		x2=parseInt(x.substr(1,1));
		var d1=parseInt(idNumber.substr(1,1));
		var d2=parseInt(idNumber.substr(2,1));
		var d3=parseInt(idNumber.substr(3,1));
		var d4=parseInt(idNumber.substr(4,1));
		var d5=parseInt(idNumber.substr(5,1));
		var d6=parseInt(idNumber.substr(6,1));
		var d7=parseInt(idNumber.substr(7,1));
		var d8=parseInt(idNumber.substr(8,1));
		var d9=parseInt(idNumber.substr(9,1));
		y=x1 + x2*9 + d1*8 + d2*7 + d3*6 + d4*5 + d5*4 + d6*3 + d7*2 + d8*1;
		checksum=10-(y%10);
		if(checksum!=d9){				
				alert("身分證字號輸入錯誤");
				setTimeout(function() {
			   $("#id_number").focus();
			}, 0);
				return false;
		}
	}else{
		if(idNumber.length>0){			
			alert("身分證字號輸入錯誤");			
			setTimeout(function() {
			   $("#id_number").focus();
			}, 0);
			return false;
		}
	}
}
function checkemail(e){
	if($('#'+e).val()!=""){
		var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(!reg.test($('#'+e).val()))
		{
			alert("郵箱格式不正確");			
			setTimeout(function() {
			  $('#'+e).focus();
			}, 0);
		}
	}else{
		if($('#'+e).val().length>0){
			alert("郵箱格式不正確");
			setTimeout(function() {
				  $('#'+e).focus();
				}, 0);
		}
	}
}

function open_email(id){

	var email=$('#'+id).val();
    //alert(email)
	if(email==''|| email==undefined){
		alert('email不能為空');
	}else{
		var regexp=/.*@.*\..*/;
		if(regexp.test(email)){
			location.href='mailto:'+email;
		}else{
			alert('email格式輸入錯誤');
		}
	}
} 

function open_website(id){
	var url=$("#"+id).val();
	if(url!=''){
		var regexp=/.*https?:\/\/*/;
		if(regexp.test(url)){
			window.open(url,"newwindow","resizable=yes,location=no");
		}else{
			var regexp=/www\..*/;
			if(regexp.test(url)){
				window.open('http://'+url,"newwindow","resizable=yes,location=no");
			}else{
				alert("網址必須以www開頭");
			}
		}
	}else{
		alert("網址不能為空");
	}
}